home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13396 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  39 lines

  1. Newsgroups: comp.lang.c
  2. Path: netcom.com!smryan
  3. From: smryan@netcom.com (@#$%!?!)
  4. Subject: Re: convert 4 8-bit bytes in a foat
  5. Message-ID: <smryanDpGp84.Fsw@netcom.com>
  6. Organization: The Programmer formerly known as S M Ryan
  7. X-Newsreader: TIN [version 1.2 PL1]
  8. References: <4k5pr7$bp7@nuscc.nus.sg>
  9. Date: Sat, 6 Apr 1996 22:31:16 GMT
  10. Sender: smryan@netcom6.netcom.com
  11.  
  12.  
  13. : I have 4 8-bit bytes. If I line them up in order, they actually represent 
  14. : the 32-bit float data type version of a decimal point number. So, how do 
  15. : I get the compiler to understand that, and print out the float number 
  16. : instead of giving me 4 integers?
  17.  
  18.  
  19. You can generally use a union to fake out type checking:
  20.  
  21.     union {float f; char b[4];} coercer;
  22.  
  23. And then depending on what order your machine stores bytes
  24.  
  25.     assert(sizeof(float)==4);
  26.  
  27.     coercer.b[0] = byte0;        coercer.b[0] = byte3;
  28.     ...            or              ...
  29.     coercer.b[3] = byte3;        coercer.b[3] = byte0;
  30.  
  31. and then use
  32.  
  33.     ...coercer.f...
  34. -- 
  35. A haggard king with hungerred sigh         | smryan@netcom.com  PO Box 1563
  36. awaits the one who wanders nigh.           |          Cupertino, California
  37. With royal robe and ruined globe,          | (xxx)xxx-xxxx            95015
  38. this phantom casts a fearful lie.          |         I don't use no smileys
  39.